package com.common.UITags;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

public class DataGridToolBarTag extends TagSupport {
	private static final long serialVersionUID = 1L;

	JspWriter out;
	StringBuffer sBF = new StringBuffer();
	private String realId;
	private String exportflag;
	private DataGridTag parent;

	@Override
	public int doStartTag() throws JspException {
		try {
			parent = (DataGridTag) this.getParent();
			realId = parent.getRealid();

			out = pageContext.getOut();
			sBF.setLength(0);

			sBF.append("<div id=\"" + realId + "\">");
			out.print(sBF.toString());
		} catch (IOException e) {
			throw new JspException("标签往前台写数据时异常");
		} catch (Exception e) {
			throw new JspException("其他异常");
		}
		return EVAL_BODY_INCLUDE;
	}

	@Override
	public int doEndTag() throws JspException {
		try {
			sBF.setLength(0);
			if ("true".equals(exportflag)) {// 如果设置了导出
				sBF.append("<a  class=\"easyui-linkbutton\" iconCls=\"icon-save\" plain=\"true\"");
				sBF.append(" onclick = \"datagridExcelExp('" + realId + "')\">");
				sBF.append("数据导出</a>");
				sBF.append("</div>");
				//将grid的结构放到了session,以便进行数据导出使用
				pageContext.getSession().setAttribute(this.realId+"_head", this.parent.getHeadList());
				pageContext.getSession().setAttribute(this.realId+"_title", this.parent.getTitle());
			}
			out.print(sBF.toString());
		} catch (IOException e) {
			throw new JspException("标签往前台写数据时异常");
		} catch (Exception e) {
			throw new JspException("其他异常");
		}
		return EVAL_PAGE;
	}

	public String getExportflag() {
		return exportflag;
	}

	public void setExportflag(String exportflag) {
		this.exportflag = exportflag;
	}


}
